package com.yyq.gxdc.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyq.gxdc.bean.Result;
import com.yyq.gxdc.entity.ReceivingAddress;
import com.yyq.gxdc.entity.User;
import com.yyq.gxdc.service.IReceivingAddressService;
import com.yyq.gxdc.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@CrossOrigin
@Controller
@RequestMapping("/receivingAddress")
public class ReceivingAddressController {

    @Autowired
    private IReceivingAddressService receivingAddressService;

    @Autowired
    private IUserService userService;

    /**
     * 分页查找 -- 根据当前登录人获取收货地址
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @GetMapping("/front/receivingAddressList")
    @ResponseBody
    public Result<IPage<ReceivingAddress>> receivingAddressList(
            HttpServletRequest request,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "8") Integer pageSize,
            Model model) {

        Result<IPage<ReceivingAddress>> result = new Result<IPage<ReceivingAddress>>();
        Page<ReceivingAddress> page = new Page<ReceivingAddress>(pageNum, pageSize);
        ReceivingAddress receivingAddress = new ReceivingAddress();
        // 获取当前登录人 信息
        Object curruser = request.getSession().getAttribute("user");
        User user = userService.getById((Integer)curruser);
        receivingAddress.setUid(user.getId());
        IPage<ReceivingAddress> allReceivingAddress = receivingAddressService.getAllReceivingAddress(page, receivingAddress);
        result.setSuccess(true);
        result.setResult(allReceivingAddress);
        return result;
    }

    /**
     * 后台分页查找
     * @param address
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @GetMapping("/mgr/receivingAddressList")
    @ResponseBody
    public Result<IPage<ReceivingAddress>> mgrReceivingAddressList(
            String address,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10000") Integer pageSize,
            Model model) {

        Result<IPage<ReceivingAddress>> result = new Result<IPage<ReceivingAddress>>();
        Page<ReceivingAddress> page = new Page<ReceivingAddress>(pageNum, pageSize);
        ReceivingAddress receivingAddress = new ReceivingAddress();
        receivingAddress.setAddress(address);
        IPage<ReceivingAddress> allRoute = receivingAddressService.getAllReceivingAddress(page, receivingAddress);
        result.setSuccess(true);
        result.setResult(allRoute);
        return result;
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @GetMapping("/getReceivingAddressDetail")
    @ResponseBody
    public Result<ReceivingAddress> getReceivingAddressDetail(int id, HttpServletRequest req) {
        Result<ReceivingAddress> result = new Result<>();
        ReceivingAddress detailByRid = receivingAddressService.getDetailByRid(id);
        result.setResult(detailByRid);
        result.setSuccess(true);
        return result;
    }

    /**
     * 添加地址管理信息
     * @param receivingAddress
     * @param req
     * @return
     */
    @PostMapping(value = "/addReceivingAddress")
    @ResponseBody
    public Result<Object> addReceivingAddress(ReceivingAddress receivingAddress, HttpServletRequest req) {
        // 新增 地址信息
        Object curruser = req.getSession().getAttribute("user");
        User user = userService.getById((Integer)curruser);
        if(user == null ){
            return Result.error("您还未登录,请先登录！");
        }else{
            receivingAddress.setUid(user.getId());
        }

        receivingAddressService.add(receivingAddress);
        return Result.ok();
    }

    /**
     * 删除地址
     * @param id
     * @param req
     * @return
     */
    @PostMapping(value = "/deleteReceivingAddress")
    @ResponseBody
    public Result<Object> deleteReceivingAddress(int id, HttpServletRequest req) {
        // 先查询 这个 地址 是否已存在
        ReceivingAddress byId = receivingAddressService.findById(id);
        if(byId == null ){
            return Result.error("该地址不存在！");
        }else{
            receivingAddressService.delete(id);
            return Result.ok();
        }
    }

    /**
     * 修改地址
     * @param receivingAddress
     * @param req
     * @return
     */
    @PostMapping(value = "/updateReceivingAddress")
    @ResponseBody
    public Result<Object> updateReceivingAddress(ReceivingAddress receivingAddress, HttpServletRequest req) {
        // 先查询 这个 地址 是否已存在
        ReceivingAddress byId = receivingAddressService.findById(receivingAddress.getId());
        if(byId == null ){
            return Result.error("该菜品不存在！");
        }else{
            receivingAddressService.update(receivingAddress);
        }
            return Result.ok();
    }

    /**
     * 编辑详情
     * @param id
     * @return
     */
    @GetMapping("/getDetailByRid")
    @ResponseBody
    public Result<ReceivingAddress> getDetailByRid(int id, HttpServletRequest req) {
        Result<ReceivingAddress> result = new Result<>();
        // 先查询 这个 菜品 是否已存在
        ReceivingAddress byId = receivingAddressService.findById(id);
        if(byId == null ){
            result.setSuccess(false);
            result.setMessage("该菜品不存在！");
            return result;
        }else{
            result.setResult(byId);
            result.setSuccess(true);
            return result;
        }
    }
}
